import request from './request'

const INTEGRATION_PREFIX = '/integration'

/**
 * 媒体系统与活动、日记模块集成API
 */

// =====================
// 活动集成相关接口
// =====================

// 将媒体文件关联到活动
export function attachMediaToActivity(options: {
  activityId: number
  mediaIds: number[]
  description?: string
  displayOrder?: number[]
}) {
  return request({
    url: `${INTEGRATION_PREFIX}/activity/${options.activityId}/media`,
    method: 'post',
    data: {
      mediaIds: options.mediaIds,
      description: options.description,
      displayOrder: options.displayOrder
    }
  })
}

// 从活动中移除媒体文件
export function detachMediaFromActivity(activityId: number, mediaIds: number[]) {
  return request({
    url: `${INTEGRATION_PREFIX}/activity/${activityId}/media`,
    method: 'delete',
    data: { mediaIds }
  })
}

// 获取活动的关联媒体
export function getActivityMedia(activityId: number, params?: {
  page?: number
  size?: number
  mediaType?: 'image' | 'video' | 'audio' | 'document'
  sortBy?: 'uploadTime' | 'displayOrder' | 'name'
}) {
  return request({
    url: `${INTEGRATION_PREFIX}/activity/${activityId}/media`,
    method: 'get',
    params
  })
}

// 更新活动媒体排序
export function updateActivityMediaOrder(activityId: number, orderList: {
  mediaId: number
  displayOrder: number
}[]) {
  return request({
    url: `${INTEGRATION_PREFIX}/activity/${activityId}/media/order`,
    method: 'put',
    data: { orderList }
  })
}

// 根据媒体查找关联的活动
export function getMediaActivities(mediaId: number) {
  return request({
    url: `${INTEGRATION_PREFIX}/media/${mediaId}/activities`,
    method: 'get'
  })
}

// =====================
// 日记集成相关接口
// =====================

// 将媒体文件关联到日记
export function attachMediaToDiary(options: {
  diaryId: number
  mediaIds: number[]
  caption?: string
  position?: 'inline' | 'attachment'
  insertPosition?: number
}) {
  return request({
    url: `${INTEGRATION_PREFIX}/diary/${options.diaryId}/media`,
    method: 'post',
    data: {
      mediaIds: options.mediaIds,
      caption: options.caption,
      position: options.position,
      insertPosition: options.insertPosition
    }
  })
}

// 从日记中移除媒体文件
export function detachMediaFromDiary(diaryId: number, mediaIds: number[]) {
  return request({
    url: `${INTEGRATION_PREFIX}/diary/${diaryId}/media`,
    method: 'delete',
    data: { mediaIds }
  })
}

// 获取日记的关联媒体
export function getDiaryMedia(diaryId: number, params?: {
  page?: number
  size?: number
  position?: 'inline' | 'attachment'
  mediaType?: 'image' | 'video' | 'audio' | 'document'
}) {
  return request({
    url: `${INTEGRATION_PREFIX}/diary/${diaryId}/media`,
    method: 'get',
    params
  })
}

// 更新日记媒体位置和标题
export function updateDiaryMediaInfo(diaryId: number, mediaId: number, updates: {
  caption?: string
  position?: 'inline' | 'attachment'
  insertPosition?: number
}) {
  return request({
    url: `${INTEGRATION_PREFIX}/diary/${diaryId}/media/${mediaId}`,
    method: 'put',
    data: updates
  })
}

// 根据媒体查找关联的日记
export function getMediaDiaries(mediaId: number) {
  return request({
    url: `${INTEGRATION_PREFIX}/media/${mediaId}/diaries`,
    method: 'get'
  })
}

// =====================
// 时间线和记忆相关接口
// =====================

// 创建时间线节点（关联媒体、活动、日记）
export function createTimelineNode(options: {
  title: string
  description?: string
  eventDate: string
  location?: {
    latitude: number
    longitude: number
    address: string
  }
  mediaIds?: number[]
  activityIds?: number[]
  diaryIds?: number[]
  tags?: string[]
}) {
  return request({
    url: `${INTEGRATION_PREFIX}/timeline`,
    method: 'post',
    data: options
  })
}

// 获取时间线
export function getTimeline(params?: {
  startDate?: string
  endDate?: string
  tags?: string[]
  includeMedia?: boolean
  includeActivities?: boolean
  includeDiaries?: boolean
  page?: number
  size?: number
}) {
  return request({
    url: `${INTEGRATION_PREFIX}/timeline`,
    method: 'get',
    params
  })
}

// 更新时间线节点
export function updateTimelineNode(nodeId: number, updates: {
  title?: string
  description?: string
  eventDate?: string
  location?: {
    latitude: number
    longitude: number
    address: string
  }
  tags?: string[]
}) {
  return request({
    url: `${INTEGRATION_PREFIX}/timeline/${nodeId}`,
    method: 'put',
    data: updates
  })
}

// 删除时间线节点
export function deleteTimelineNode(nodeId: number) {
  return request({
    url: `${INTEGRATION_PREFIX}/timeline/${nodeId}`,
    method: 'delete'
  })
}

// =====================
// 智能关联和推荐接口
// =====================

// 基于时间和地点的智能关联建议
export function getSmartAssociations(mediaIds: number[]) {
  return request({
    url: `${INTEGRATION_PREFIX}/smart-associations`,
    method: 'post',
    data: { mediaIds }
  })
}

// 推荐相关内容（基于标签、时间、地点等）
export function getRelatedContent(params: {
  mediaId?: number
  activityId?: number
  diaryId?: number
  limit?: number
}) {
  return request({
    url: `${INTEGRATION_PREFIX}/recommendations`,
    method: 'get',
    params
  })
}

// 创建记忆合集（自动整理相关的媒体、活动、日记）
export function createMemoryCollection(options: {
  title: string
  description?: string
  timeRange: {
    startDate: string
    endDate: string
  }
  tags?: string[]
  locations?: string[]
  autoInclude?: {
    media: boolean
    activities: boolean
    diaries: boolean
  }
}) {
  return request({
    url: `${INTEGRATION_PREFIX}/memory-collection`,
    method: 'post',
    data: options
  })
}

// 获取记忆合集
export function getMemoryCollections(params?: {
  page?: number
  size?: number
  sortBy?: 'createTime' | 'eventTime' | 'title'
  tags?: string[]
}) {
  return request({
    url: `${INTEGRATION_PREFIX}/memory-collections`,
    method: 'get',
    params
  })
}

// =====================
// 批量操作接口
// =====================

// 批量关联媒体到多个内容
export function batchAssociateMedia(options: {
  mediaIds: number[]
  targets: {
    activityIds?: number[]
    diaryIds?: number[]
    timelineNodeIds?: number[]
  }
}) {
  return request({
    url: `${INTEGRATION_PREFIX}/batch-associate`,
    method: 'post',
    data: options
  })
}

// 批量移除关联
export function batchRemoveAssociations(options: {
  mediaIds: number[]
  targets: {
    activityIds?: number[]
    diaryIds?: number[]
    timelineNodeIds?: number[]
  }
}) {
  return request({
    url: `${INTEGRATION_PREFIX}/batch-remove-associations`,
    method: 'post',
    data: options
  })
}

// =====================
// 统计和分析接口
// =====================

// 获取集成统计信息
export function getIntegrationStatistics() {
  return request({
    url: `${INTEGRATION_PREFIX}/statistics`,
    method: 'get'
  })
}

// 获取内容关联图谱
export function getContentGraph(params?: {
  centerType: 'media' | 'activity' | 'diary'
  centerId: number
  depth?: number
  includeTypes?: ('media' | 'activity' | 'diary')[]
}) {
  return request({
    url: `${INTEGRATION_PREFIX}/content-graph`,
    method: 'get',
    params
  })
}

/**
 * TypeScript 类型定义
 */

export interface ActivityMediaAssociation {
  id: number
  activityId: number
  mediaId: number
  description?: string
  displayOrder: number
  createTime: Date
  media: {
    id: number
    fileName: string
    thumbnailUrl?: string
    fileType: string
    fileSize: number
  }
}

export interface DiaryMediaAssociation {
  id: number
  diaryId: number
  mediaId: number
  caption?: string
  position: 'inline' | 'attachment'
  insertPosition?: number
  createTime: Date
  media: {
    id: number
    fileName: string
    thumbnailUrl?: string
    fileType: string
    fileSize: number
  }
}

export interface TimelineNode {
  id: number
  title: string
  description?: string
  eventDate: Date
  location?: {
    latitude: number
    longitude: number
    address: string
  }
  tags: string[]
  mediaCount: number
  activityCount: number
  diaryCount: number
  createTime: Date
  updateTime: Date
  associatedMedia?: MediaItem[]
  associatedActivities?: ActivityItem[]
  associatedDiaries?: DiaryItem[]
}

export interface MediaItem {
  id: number
  fileName: string
  thumbnailUrl?: string
  fileType: string
  fileSize: number
  uploadTime: Date
  tags?: string[]
}

export interface ActivityItem {
  id: number
  title: string
  description?: string
  activityDate: Date
  location?: string
  participantCount: number
}

export interface DiaryItem {
  id: number
  title: string
  content: string
  createTime: Date
  mood?: string
  weather?: string
}

export interface SmartAssociation {
  type: 'activity' | 'diary' | 'timeline'
  targetId: number
  title: string
  description: string
  confidence: number // 0-1 置信度
  reason: string // 关联原因
  suggestedAction: string
  relatedItems: {
    media: MediaItem[]
    activities?: ActivityItem[]
    diaries?: DiaryItem[]
  }
}

export interface MemoryCollection {
  id: number
  title: string
  description?: string
  timeRange: {
    startDate: Date
    endDate: Date
  }
  tags: string[]
  locations: string[]
  mediaCount: number
  activityCount: number
  diaryCount: number
  coverImage?: string
  createTime: Date
  updateTime: Date
  items: {
    media: MediaItem[]
    activities: ActivityItem[]
    diaries: DiaryItem[]
    timelineNodes: TimelineNode[]
  }
}

export interface ContentGraphNode {
  id: string
  type: 'media' | 'activity' | 'diary' | 'timeline'
  title: string
  description?: string
  thumbnail?: string
  createTime: Date
  connections: ContentGraphEdge[]
}

export interface ContentGraphEdge {
  source: string
  target: string
  relation: string
  weight: number
  properties?: Record<string, any>
}

export interface IntegrationStatistics {
  totalAssociations: number
  mediaWithActivities: number
  mediaWithDiaries: number
  activitiesWithMedia: number
  diariesWithMedia: number
  timelineNodes: number
  memoryCollections: number
  averageAssociationsPerMedia: number
  topTags: { tag: string; count: number }[]
  recentActivity: {
    date: string
    associationsCreated: number
    memoryCollectionsCreated: number
  }[]
}